﻿using NWNT.Dal;
using NWNT.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace NWNT.Web.Controllers
{
    public class MasterController : ApiController
    {
         private IUnitOfWork uow;
         private CustomPrincipal UserDetails
         {
             get
             {
                 return ((CustomPrincipal)System.Web.HttpContext.Current.User);
             }
         }

         public MasterController()
        {
            this.uow = new UnitOfWork();
        }
        [System.Web.Http.HttpPost]
        public JsonResult UpdateQuestionAreaOrder([FromBody] IEnumerable<QuestionAreaModel> questionAreas)
        {
            var result = new JsonResult();
            string commaSeperatedIds=string.Empty;
            foreach (QuestionAreaModel model in questionAreas)
            {
                if (string.IsNullOrEmpty(commaSeperatedIds))
                {
                    commaSeperatedIds = model.QuestionAreaId.ToString();
                }
                else
                {
                    commaSeperatedIds = commaSeperatedIds + "," + model.QuestionAreaId.ToString();
                }
            }
            uow.Context.UpdateQuestionAreaOrder(commaSeperatedIds);
            
            result.Data = "Success";
            return result;
        }
        [System.Web.Http.HttpPost]
        public JsonResult UpdateQuestionArea([FromBody] QuestionAreaModel questionArea)
        {
            var result = new JsonResult();
            if (ModelState.IsValid)
            {
                if (questionArea.QuestionAreaId > 0)
                {
                    uow.Context.UpdateQuestionArea(questionArea.QuestionAreaId, questionArea.Area, questionArea.Description, null, null, UserDetails.UserId, DateTime.Now);
                }
                else
                {
                    uow.Context.UpdateQuestionArea(null, questionArea.Area, questionArea.Description, UserDetails.UserId, DateTime.Now, UserDetails.UserId, DateTime.Now);
                }
                var itemsToDelete = uow.QuestionnaireAreaOptionMappingRepository.GetAll().Where(e => e.QuestionnaireAreaId == questionArea.QuestionAreaId);
                foreach (var item in itemsToDelete)
                {
                    uow.QuestionnaireAreaOptionMappingRepository.Delete(item);
                }
                //uow.Save();
                for (int i = 0; i < questionArea.SelectedQuestionOptions.Count(); i++)
                {
                    uow.QuestionnaireAreaOptionMappingRepository.Insert(new Dal.Database.QuestionnaireAreaOptionMapping()
                    {
                        CreatedBy = UserDetails.UserId,
                        CreatedOn = DateTime.Now,
                        EditedBy = UserDetails.UserId,
                        EditedOn = DateTime.Now,
                        OptionId = questionArea.SelectedQuestionOptions.ElementAt(i),
                        Order = i + 1,
                        QuestionnaireAreaId = questionArea.QuestionAreaId,
                        Score = questionArea.SelectedQuestionOptionsScores.ElementAt(i)
                    });
                }
                uow.Save();
                result.Data = "Success";
                return result;
            }
            else
            {
                result.Data = "failure";
                return result;
            }
        }
      
    }
}
